package dst.ass1.jpa.dao.impl;

import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;

import dst.ass1.jpa.dao.ITaskDAO;
import dst.ass1.jpa.model.ITask;
import dst.ass1.jpa.model.TaskStatus;
import dst.ass1.jpa.model.impl.Task;
import dst.ass1.jpa.model.impl.TaskProcessing;

public class TaskDAO extends GenericJpaDAO <ITask> implements ITaskDAO {

	private final static Logger LOGGER = Logger.getLogger(TaskDAO.class.getName());
	
	public TaskDAO(Session session) {
        super(session);
    }

    @Override
    protected String getTableName() {
        return Task.class.getSimpleName();
    }
	
    
    /**
     * 1.2.c
     * Find all tasks that were created by a specified user which have a given description (user is
     * given by his username).
     * Use of Hibernate Criteria API
     */
	@Override
	public List<ITask> findTasksForUserAndContext(String user, String context) {
		
		LOGGER.info("findTasksForUserAndContext called");
		
		Criteria cr = getSession().createCriteria(Task.class)		//query over Tasks
                .createAlias("user", "u")				//alias for querying
                .createAlias("metadata", "m");			//alias for querying

        if (user != null && !user.isEmpty()) {
            cr.add(Restrictions.eq("u.username", user));	//u.username with that name
        }

        if (context != null && !context.isEmpty()) {
            cr.add(Restrictions.eq("m.context", context));		//context with that description
        }

        @SuppressWarnings("unchecked")
        List<ITask> results = cr.list();					//alls Tasks that match this criteria
        
        LOGGER.info("findTasksForUserAndContext "+results.size() +" elements received");
        
        return results;
	}

	/**
	 * 1.2.c
	 * Query-by-Example to find all tasks with TaskStatus FINISHED and a specified start
	 * and Finish date.
	 * -- Use of Query by Example with Hibernate Criteria API --
	 */
	@Override
	public List<ITask> findTasksForStatusFinishedStartandFinish(Date start,Date finish) {
		
		LOGGER.info("findTasksForStatusFinishedStartandFinish called");
		
		TaskProcessing processing = new TaskProcessing();  //TaskProcessing that is searched
		processing.setStatus(TaskStatus.FINISHED);  	   //status that is searched
		processing.setStart(start);						   //set Start Date
        processing.setEnd(finish);							//set End Date
        
        Task task = new Task();			//Task that contains the searched TaskProcessing
        task.setTaskProcessing(processing);				//set TaskProcessing

        Criteria c = getSession().createCriteria(Task.class)	//Search for Tasks
                .add(Example.create(task))						//with that Example created
                .createCriteria("taskProcessing")				//critieria added
                .add(Example.create(task.getTaskProcessing()));   //add Example for taskProcessing

        @SuppressWarnings("unchecked")
        List<ITask> results = c.list();		//list all Tasks
        
        LOGGER.info("findTasksForStatusFinishedStartandFinish "+results.size() +" elements received");

        return results;
	}

}
